var rootPath = getRootPath();

$(function() {
	var state = $("#state").val();
	$.ajax({
		url: rootPath + '/realNameAuth/selectPageAuth',
        type: 'post',
        contentType : 'application/json;charset=UTF-8',
        data: JSON.stringify({
        	"dr" : 1,
        	"state" : state
        }),
        success: function (data) {
        	fillAuths(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
	
	$("#realName").keyup(function(event) {
		if(event.keyCode == 13) {
			$("#searchRealNameAuth").click();
		}
	});
	$("#searchRealNameAuth").click(function() {
		var uid = $("#uid").val();
		var realName = $("#realName").val();
		var creType = $("#creType").val();
		var state = $("#state").val();
		$.ajax({
			url: rootPath + '/realNameAuth/selectPageAuth',
	        type: 'POST',
	        data : JSON.stringify({
	        	"dr" : 1,
	        	"uid" : uid,
	        	"realName" : realName,
	        	"creType" : creType,
	        	"state" : state
	        }),
	        contentType : 'application/json;charset=UTF-8',
	        success: function (data) {
	        	$("#auths").empty();
	        	fillAuths(data);
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	layer.alert("error:(");
	        }
	    });
	});
});

/*实名认证-添加-编辑*/
function realNameAuth_add_edit(gname,url,w,h){
	layer_show(gname,url,w,h);
}

function realNameAuth_del(obj,id){
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('实名认证删除须谨慎，确认要删除吗？',function(index){
		if(id != "") {
			$.ajax({
				url: rootPath + '/realNameAuth/' + id,
				type: 'DELETE',
				success: function(data){
					if(data.returnCode == "0000") {						
						$(obj).parents("tr").remove();
						layer.msg('已删除!',{icon:1,time:1000});
						
						$("#dataNum").html(dataNum - 1);
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error:function(data) {
					layer.alert("error:(");
				},
			});		
		}
	});
}

function fillAuths(data) {
	if(data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr;
		for(var i = 0;i < length;i++) {
			tr += "<tr class='text-c' name='realNameAuthTr'>";
			tr += "<td>" + data.returnObject.list[i].uid + "</td>";
			tr += "<td><a onclick=selectUser('" + data.returnObject.list[i].uid + "')>" + data.returnObject.list[i].realName + "</a></td>";
			if(data.returnObject.list[i].creType == 0) {				
				tr += "<td>身份证</td>";
			} else {
				tr += "<td>护照</td>";				
			}
			tr += "<td>" + formatState(data.returnObject.list[i].state) + "</td>";
			tr += "<td>" + (data.returnObject.list[i].autoCheckResult == undefined ? "" : data.returnObject.list[i].autoCheckResult) + "</td><td>";
			tr += "<a gname='编辑' href='javascript:;' onclick='realNameAuth_add_edit(\"编辑认证\"," + "\"realNameAuthEditPage?uid=" + data.returnObject.list[i].uid + "\", \"\",\"\")' style='text-decoration:none'><i class='iconfont ml-5 marginLeft12'>&#xe60a;</i></a></td></tr>";
//			tr += "<a gname='删除' href='javascript:;' onclick='realNameAuth_del(this," + "\"" + data.returnObject.list[i].uid + "\")' class='ml-5' style='text-decoration:none'><i class='Hui-iconfont'>&#xe6e2;</i></a></td></tr>";
		}
		$("#auths").empty();
		$("#auths").append(tr);
		
		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";
		$("#page").empty();
		$("#page").append(page);
		
		$(".pageNavigate").each(function() {
			$(this).hover(function() {
				$(this).css("cursor","pointer");
			});
		});
	} else {
		$("#page").html("");
		$("#dataNum").html("0");
	}
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	var uid = $("#uid").val();
	var realName = $("#realName").val();
	var creType = $("#creType").val();
	var state = $("#state").val();
	$.ajax({
		url: rootPath + '/realNameAuth/selectPageAuth',
        type: 'post',
        contentType : 'application/json;charset=UTF-8',
		data : JSON.stringify({
			"dr" : 1,
			"uid" : uid,
			"realName" : realName,
			"creType" : creType,
			"state" : state,
			"pageNo" : pageNo
		}),
		success : function(data) {
			fillAuths(data);
			$(".pageNavigate").hover(function() {
				$(this).css("cursor", "pointer");
			});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function formatState(state) {
	var value = "";
	switch(state) {
		case 1:
			value = "认证中";
			break;
		case 2:
			value = "认证成功";
			break;
		case 3:
			value = "认证失败";
			break;
	}
	return value;
}

/**
 * 打开"用户管理"
 * @param uid
 */
function selectUser(uid) {
	parent.$("iframe").each(function() {
		var src = $(this).attr("src");
		if("userPage" == src) {
			$(this).parents("div .show_iframe").remove();
		}
	});
	
	parent.$("#Hui-tabNav").find("span").each(function() {
		var href = $(this).data("href");
		if(href.startWith("userPage")) {
			$(this).parents("li").remove();
		}
	});
	creatIframe("userPage?uid=" + uid,"用户管理");
}